// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose:  Implementation of the CImplOutput class..
//   $Id$


#include "stdafx.h"
#include "output.h"
#include "OutputUI.h"
#include "formcontrol.h"


CImplOutput::CImplOutput(CXFormsElementOutput* pElement)
: CImplMultipleTextSource((CXFormsElement *) pElement)
{
	CFormControl::SetControlType(L"<output>");
}

CImplOutput::~CImplOutput()
{
}

HRESULT CImplOutput::init(CImpl * pParent)
{
	//if ref is in its default state.
	if((!!m_sValue) && ::isEmpty(m_sRef))
	{
		//TEMP, FUDGE, copy value into ref
		m_sRef = m_sValue;
		m_sValue.Empty();
		//m_bMustBindToNode = false;
		m_bCanBindToValue = true;
	}
	else
	{
		m_bCanBindToValue = false;
	}
	return __super::init(pParent);
}

HRESULT CImplOutput::refresh()
{
	HRESULT hr;
	if(m_bCanBindToValue)
	{
		if(!m_sCurrentValue)
		{
			hr = GetBoundData(&m_sCurrentValue);
		}

		if(m_sCurrentValue)
		{
			hr = m_pXFormsElement->put_value(m_sCurrentValue.m_str);		
		}
		m_sCurrentValue.Empty();
	}
	else
	{
		hr = __super::refresh();
	}
	return hr;
}

HRESULT CImplOutput::onvaluechanged()
{
	HRESULT hr;
	if(m_bCanBindToValue)
	{
		//ignore
		hr = S_FALSE;
	}
	else
	{
		hr = __super::onvaluechanged();
	}
	return hr;
}

HRESULT CImplOutput::recalculateValue()
{
	HRESULT hr = S_FALSE;
	if(m_bCanBindToValue)
	{
		m_sCurrentValue.Empty();
		hr = GetBoundData(&m_sCurrentValue);
	}
	return hr;
}
